#! /usr/bin/env python # Port of the classic Guess My Number to the PSP changelog = """ 0.1 Fixed crash when exiting 0.1.1 Fixed bug when restarting """ import random, psp2d, os from time import time #CLEAR_COLOR = psp2d.Color(0,0,0) abspath = os.path.abspath(__file__) path = os.path.dirname(abspath) + "/" CLEAR_COLOR = psp2d.Color(150,150,150) scr = psp2d.Screen() fnt = psp2d.Font(path+"font.png") img = psp2d.Image(480, 272) img_list = [psp2d.Image(path+"level_select.png"),psp2d.Image(path+"bg.png")] max_num = input the_number = input input_num = 0 x = input class guess: def select_level(self): items = [0,1,2,3] pos = 1 global max_num def update(): img.blit(img_list[0]) fnt.drawText(img, 50, 30, "Easy (up to 10)") fnt.drawText(img, 50, 60, "Normal (up to 100)") fnt.drawText(img, 50, 90, "Hard (up to 1000)") fnt.drawText(img, 0, 30*pos, " >") scr.blit(img) scr.swap() def work(): global max_num if items[pos] == 1: max_num = 10 elif items[pos] == 2: max_num = 100 elif items[pos] == 3: max_num = 1000 self.get_random_number() img.blit(img_list[0]) fnt.drawText(img, 50, 30, "Easy (up to 10)") fnt.drawText(img, 50, 60, "Normal (up to 100)") fnt.drawText(img, 50, 90, "Hard (up to 1000)") fnt.drawText(img, 0, 30*pos, " >") scr.blit(img) scr.swap() x3 = True while x3 == True: self.delay() pad = psp2d.Controller() if pad.down: pos += 1 if pos > 3: pos = 1 if pos == 0: pos = 1 update() elif pad.up: pos -= 1 if pos == 0: pos = 3 update() elif pad.cross: x3 = False work() elif pad.circle: x3 = False fnt.drawText(img, 50, 250, "Exiting...") scr.blit(img) scr.swap() def help(self): img.blit(img_list[1]) fnt.drawText(img, 100, 60, "Up/down to add/substract 1") fnt.drawText(img, 100, 75, "Right/Left to add/substract 10") fnt.drawText(img, 100, 90, "R/L-trigger to add/substract 100") fnt.drawText(img, 100, 105, "Cross to check") fnt.drawText(img, 100, 120, "Circle to exit") scr.blit(img) scr.swap() x2 = True while x2 == True: self.delay() if psp2d.Controller().select: x2 = False self.guess() def delay(self): time_lapse = 0.17 time_start = time() time_end = (time_start + time_lapse) while time_end > time(): pass def delay_img(self): time_lapse = 0.20 time_start = time() time_end = (time_start + time_lapse) while time_end > time(): pass def get_random_number(self): global the_number, max_num the_number = random.randrange(max_num) self.guess() def update_scr(self): global input_num, max_num img.blit(img_list[1]) if input_num < 0: input_num = max_num elif input_num > max_num: input_num = 0 fnt.drawText(img, 0, 0, "Press select for help") fnt.drawText(img, 200, 200, str(input_num)) scr.blit(img) scr.swap() def check(self): global the_number, input_num if input_num > the_number: img.blit(img_list[1]) fnt.drawText(img, 100, 50, "Less!") scr.blit(img) scr.swap() self.delay_img() self.guess() elif input_num < the_number: img.blit(img_list[1]) fnt.drawText(img, 100, 50, "More!") scr.blit(img) scr.swap() self.delay_img() self.guess() elif input_num == the_number: fnt.drawText(img, 100, 50, "Correct! the number was "+str(the_number)) fnt.drawText(img, 100, 70, "Cross to start again") fnt.drawText(img, 100, 90, "Circle to exit") scr.blit(img) scr.swap() self.delay_img() x1 = True while x1 == True: self.delay() pad = psp2d.Controller() if pad.cross: x1 = False self.select_level() elif pad.circle: x1 = False fnt.drawText(img, 100, 120, "Exiting...") scr.blit(img) scr.swap() def guess(self): global input_num img.blit(img_list[1]) fnt.drawText(img, 0, 0, "Press select for help") fnt.drawText(img, 200, 200, str(input_num)) scr.blit(img) scr.swap() x = True while x == True: self.delay() pad = psp2d.Controller() if pad.up: input_num += 1 self.update_scr() elif pad.down: input_num -= 1 self.update_scr() elif pad.right: input_num += 10 self.update_scr() elif pad.left: input_num -= 10 self.update_scr() elif pad.r: input_num += 100 self.update_scr() elif pad.l: input_num -= 100 self.update_scr() elif pad.cross: x = False self.check() elif pad.circle: x = False fnt.drawText(img, 100, 120, "Exiting...") scr.blit(img) scr.swap() elif pad.select: self.help() def main(): guess().select_level()